/**
 *******************************************************************************
 * @FileName  : hci_tr_port.c
 * @Author    : GaoQiu
 * @CreateDate: 2020-09-10
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms desc ribed
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#include "hci_tr_port.h"
#include "hci_tr_api.h"

volatile uint8_t isUartTxDone = 1;
void HCI_TR_UartInit(uint8_t *pRxBuf, uint32_t byteSize)
{
	/* UART/DMA Clock config */
    RCC_EnablePeriphClk(RCC_UART|RCC_DMA);

    /**
     * !!!Note: pull-up setting must before UART GPIO config,
     * cause it will lead to ERR data to UART RX buffer
     */
    GPIO_SetPullModeEx(HCI_TR_RX_PIN, GPIO_PullUp_Strong);//must, In order to stabilize and prevent leakage
    GPIO_SetPullModeEx(HCI_TR_TX_PIN, GPIO_PullUp_Strong);//must, In order to stabilize and prevent leakage

    /* UART TX/RX Pin config */
    GPIO_InitEx(HCI_TR_RX_PIN, GPIO_Mode_AF_In);
    GPIO_SetAlternFuncEx(HCI_TR_RX_PIN, GPIO_AF_UART);

    GPIO_InitEx(HCI_TR_TX_PIN, GPIO_Mode_AF_In);
    GPIO_SetAlternFuncEx(HCI_TR_TX_PIN, GPIO_AF_UART);

    /* UART initialization */
	UART_InitType_t UART_InitStruct;
	UART_InitStruct.SysClkFreq      = HCI_TR_SYS_CLK;
	UART_InitStruct.UART_BaudRate   = HCI_TR_BAUDRATE;
	UART_InitStruct.UART_StopBits   = UART_StopBits_1;
	UART_InitStruct.UART_Parity     = UART_Parity_No;
	UART_InitStruct.UART_HwFlowCtrl = UART_HwFlowCtrl_None;
	UART_Init(&UART_InitStruct);

	/* UART Rx/Tx use DMA mode. */
	if(pRxBuf != NULL && byteSize != 0){
		UART_DmaConfig(pRxBuf, byteSize);
	}
	else{
		HCI_TR_TRACK_ERR("UART initialization failed...\r\n");
	}
	//UART_DmaITConfig(UART_DMA_IT_Rx_Tx, ENABLE);
	UART_DmaITConfig(UART_DMA_IT_Rx, ENABLE);
	IRQ_EnablePeriphIrq(IRQ_DMA);
	UART_DmaCmd(UART_DMA_Rx_Tx, ENABLE);

#if 1
	UART->TIMOUT |= 0xC000;  //Enable UART Tx done and Err IRQ
	UART->STATUS2 = BIT(7);  //Clear Tx done status
	IRQ_EnablePeriphIrq(IRQ_UART);
	isUartTxDone = 1;
#endif
}

#if UART_TEST_EN
uint8_t A_rxBuf[128];

void HCI_TR_UartTestInit(void)
{
	HCI_TR_UartInit(A_rxBuf, sizeof(A_rxBuf));
}

RAM_CODE void UART_IRQHandler(void)
{
	/* UART DMA Rx IRQ */
	if(UART_GetDmaITFlagState(UART_DMA_IT_Flag_RxDone))
	{
		UART_ClearDmaITFlagState(UART_DMA_IT_Flag_RxDone);

		UART_SendDataByDma(A_rxBuf);
	}

	/* UART DMA Tx IRQ */
	if(UART_GetDmaITFlagState(UART_DMA_IT_Flag_TxDone))
	{
		UART_ClearDmaITFlagState(UART_DMA_IT_Flag_TxDone);
	}
}
#endif

